<template>
  <div class="app-container">
    <span> 活动类型： </span>
    <template>
      <el-select v-model="type" placeholder="请选择类型"  style="margin: 10px">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </template>
    <el-button type="primary" round @click="fetchData(page)">搜索</el-button>

    <el-button type="primary" plain @click="dialogCreateGiftVisible = true; ">添加礼物</el-button>
    <el-button type="primary" plain @click="dialogUpdateMoneyVisible = true; ">修改垫盘金币</el-button>
    <el-button type="primary" plain @click="dialogUpdateMoneyProbabilityVisible = true; ">修改出奖率</el-button>

    <div v-if="type === '7'">
      <span> 上期已抽数量:</span>
      {{ lastAstraNumber.lotteryCount }}
      <span> 上期总数量:</span>
      {{ lastAstraNumber.totalCycleCount }}

      <span> 上期用户总消费:</span>
      {{ lastAstraNumber.consumeNumber }}

      <span> 上期礼物总价值:</span>
      {{ lastAstraNumber.giftNumber }}

      <span> 上期平台总盈利:</span>
      {{ lastAstraNumber.consumeNumber-lastAstraNumber.giftNumber }}
      <br>
      <span> 本期已抽数量:</span>
      {{ astraNumber.lotteryCount }}
      <span> 本期总数量:</span>
      {{ astraNumber.totalCycleCount }}

      <span> 用户总消费:</span>
      {{ astraNumber.consumeNumber }}

      <span> 礼物总价值:</span>
      {{ astraNumber.giftNumber }}

      <span> 平台总盈利（垫盘金）:</span>
      {{ astraNumber.consumeNumber-astraNumber.giftNumber }}

      <span> 出奖率: </span>
      {{ astraNumber.rebateRatio }}
      <br>
    </div>

    <el-table
      v-if="type === '6'"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="礼物名称" width="150">
        <template slot-scope="scope">
          {{ scope.row.giftName }}
        </template>
      </el-table-column>
      <el-table-column label="礼物图片" width="200" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.giftIcon"
            :preview-src-list="[scope.row.giftIcon]"
          />
        </template>
      </el-table-column>
      <el-table-column label="礼物价格" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.giftPrice}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="已投数量" width="110">
        <template slot-scope="scope">
          {{ scope.row.currentNumber }} /  {{ scope.row.totalNumber }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="110" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="dialogViewListVisible = true; getFoodStallItemList(scope.row.id);"
          >
            查看所有批次
          </el-button>
          <el-button size="mini" type="primary" round @click="dialogViewListVisible = true; updateUserProfit.userId = scope.row.id">
            关闭
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-table
      v-if="type === '7'"
      v-loading="listLoading"
      :data="adAstraList"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="星球名称" width="150">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="倍率" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.magnification }}
        </template>
      </el-table-column>
      <el-table-column label="中奖概率" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.probability}}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="110" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="dialogUpdateAdAstraVisible = true; updateAstra.id = scope.row.id; updateAstra.probability = scope.row.probability"
          >
            修改概率
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog title="查看所有批次" :visible.sync="dialogViewListVisible" width="76%">
      <el-table
        v-loading="itemListLoading"
        style="margin-top: 10px"
        :data="itemList"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column align="center" label="ID" width="110">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column label="批次" width="150">
          <template slot-scope="scope">
            第{{ scope.row.number }}批
          </template>
        </el-table-column>

        <el-table-column align="center" prop="created_at" label="开奖时间" width="200">
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span>{{ scope.row.updateTime }}</span>
          </template>
        </el-table-column>

        <el-table-column label="参与人员" width="150">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              @click="dialogViewUserListVisible = true; getFoodStallUserList(scope.row.id);"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="110" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              circle
              @click="handelDeleteTopic(scope.row.id)"
            />
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogViewListVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="参与人员" :visible.sync="dialogViewUserListVisible" width="76%">
      <el-table
        v-loading="userListLoading"
        style="margin-top: 10px"
        :data="userList"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column align="center" label="参与人员" width="110">
          <template slot-scope="scope">
            {{ scope.row.nickname }}
          </template>
        </el-table-column>
        <el-table-column label="用户id" width="150">
          <template slot-scope="scope">
            {{ scope.row.account }}
          </template>
        </el-table-column>

        <el-table-column label="投入数量" width="150">
          <template slot-scope="scope">
            {{ scope.row.count }}
          </template>
        </el-table-column>

        <el-table-column label="状态" width="150">
          <template slot-scope="scope">
            {{ scope.row.status == '-1'?'失败': '成功'}}
          </template>
        </el-table-column>

        <el-table-column align="center" prop="created_at" label="投入时间" width="200">
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogViewUserListVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑奖励" :visible.sync="dialogEditVisible" width="76%">
      <el-form ref="form" :model="giftDTO" label-width="80px">
        <el-form-item label="礼物名称">
          <el-input v-model="giftDTO.name" />
        </el-form-item>
        <el-form-item label="礼物价格">
          <el-input-number v-model="giftDTO.price" :min="1" :max="100000000" label="描述文字"></el-input-number>
        </el-form-item>

        <el-form-item label="是否上架">
          <el-switch
            v-model="giftDTO.status"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="礼物类型">
          <el-select v-model="giftDTO.type" placeholder="礼物类型" style="margin: 10px">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="礼物图标">
          <el-upload
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            list-type="picture-card"
            :limit="1"

            :on-success="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="giftDTO.icon" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="礼物特效">
          <el-upload
            class="upload-demo"
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            :on-remove="handleRemove2"
            :on-success="handlePictureCardPreview2"
            multiple
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传 svga 文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="giftDTO.sort" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCreateGiftVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAddGfit">确 定</el-button>
      </div>
    </el-dialog>


    <el-dialog title="修改概率" :visible.sync="dialogUpdateAdAstraVisible" width="76%">
      <el-form ref="form" :model="updateAstra" label-width="80px">
        <el-form-item  label="奖品概率">
          <el-input-number v-model="updateAstra.probability" :min="0.01" :max="100000000" label="描述文字" />%
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUpdateAdAstraVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateAstra">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改出奖出奖最大比率" :visible.sync="dialogUpdateMoneyProbabilityVisible" width="76%">
      <el-form ref="form" :model="updateAstra" label-width="80px">
        <el-form-item  label="出奖最大比率">
          <el-input-number v-model="updateAstra.rebateRatio" :min="0.01" :max="100000000" label="描述文字" />%
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUpdateMoneyProbabilityVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateAstraRebateRatio">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改垫盘金" :visible.sync="dialogUpdateMoneyVisible" width="76%">
      <el-form ref="form" :model="updateAstraMoney" label-width="80px">
        <div>
          <el-input
            v-model="updateAstraMoney.money"
            style="width: 150px; padding: 10px"
            placeholder="请输入修改金额"></el-input>
        </div>
        <el-radio v-model="updateAstraMoney.status" label="1">增加</el-radio>
        <el-radio v-model="updateAstraMoney.status" label="-1">减少</el-radio>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUpdateMoneyVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateAstraMoney">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData">
    </el-pagination>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import { deleteById, addGift,getActivityList,getFoodStallItemList,getFoodStallUserList,updateAstra, getAstraNumber,getLastAstraNumber,updateAstraMoney,updateAstraRebateRatio } from '@/api/party'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      options: [{
        value: '6',
        label: '吃货大排档'
      }, {
        value: '7',
        label: '星际探险'
      }],
      fileList: [],
      itemList: [],
      userList: [],
      adAstraList:[],
      updateAstra: {
        id: null,
        probability: null,
        rebateRatio: null
      },
      updateAstraMoney: {
        status: 0,
        money: 0
      },
      dialogViewUserListVisible: false,
      dialogUpdateAdAstraVisible: false,
      dialogUpdateMoneyProbabilityVisible: false,
      dialogViewListVisible: false,
      list: null,
      adAstraListLoading: false,
      listLoading: true,
      itemListLoading: true,
      dialogUpdateMoneyVisible: false,
      userListLoading: true,
      status: '-10',
      page: 1,
      size: 10,
      astraNumber: {},
      lastAstraNumber: {},
      total: 0,
      giftDTO: {
        name: '',
        circleType: '0',
        sort: 0,
        status: false,
        icon: '',
        price: '',
        materialUrl: ''
      },
      type: '7',
      dialogEditVisible: false
    }
  },
  created() {
    this.fetchData(this.page)
  },
  methods: {
    handlePictureCardPreview2(response, file) {
      this.giftDTO.materialUrl = response.data.url
      console.log(this.giftDTO.materialUrl)
    },
    handlePictureCardPreview(response, file) {
      this.giftDTO.icon = response.data.url
    },
    handleRemove2(file, fileList) {
      this.fileList = []
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    handleRemove(file) {
      this.giftDTO.icon = ''
      console.log(file)
    },
    handleClick(tab, event) {
      this.fetchData(this.page)
    },
    handleUpdateAstraRebateRatio() {
      this.dialogUpdateMoneyProbabilityVisible = false;
      updateAstraRebateRatio({probability: this.updateAstra.rebateRatio}).then(res=>{
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
      })
    },
    handleUpdateAstraMoney() {
      this.dialogUpdateMoneyVisible = false;
      updateAstraMoney({status: this.updateAstraMoney.status, money: this.updateAstraMoney.money}).then(res => {
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
      })
    },
    handleUpdateAstra() {
      this.dialogUpdateAdAstraVisible = false
      updateAstra(this.updateAstra).then(res => {
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
      })
    },
    getLastAstraNumber() {
      getLastAstraNumber().then(res => {
        this.lastAstraNumber = res.data
      })
    },
    getAstraNumber() {
      getAstraNumber().then(res => {
        this.astraNumber = res.data
      })
    },
    handleAddGfit() {
      this.dialogCreateGiftVisible = false;
      this.fileList = [];
      addGift(this.giftDTO).then(res => {
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '创建成功!'
        })
      })
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      this.getAstraNumber()
      this.getLastAstraNumber()
      getActivityList({ page: this.page, size: this.size, type: this.type }).then(response => {
        if (this.type === '7') {
          this.adAstraList = response.data
          this.listLoading = false
        }
        if (this.type === '6') {
          this.list = response.data
          this.listLoading = false
        }
        this.total = response.total
      })
    },
    getFoodStallUserList(id) {
      getFoodStallUserList({ id: id }).then(response => {
        this.userList = response.data
        this.userListLoading = false
      })
    },
    getFoodStallItemList(id) {
      getFoodStallItemList({ id: id }).then(response => {
        this.itemList = response.data
        this.itemListLoading = false
      })
    },
    handelDelete(id) {
      this.$confirm('此操作将删除该动态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handelRealDelete(id) {
      this.$confirm('此操作将彻底删除该动态,请谨慎操作！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        realDeleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
